<?php


namespace app\api\model;

use app\common\library\IDMan;
use app\common\model\BaseModel;
use common;
use think\Db;

class UserModel
{
    var $_selectlist = [
        'alipay_user_id' => 'string',
        'alipay_avatar' => 'string',
        'alipay_province' => 'string',
        'alipay_city' => 'string',
        'alipay_nickname' => 'string',
        'alipay_is_student_certified' => 'string',
        'alipay_user_type' => 'string',
        'alipay_user_status' => 'string',
        'alipay_is_certified' => 'string',
        'alipay_gender' => 'string',
        'name' => 'string',
        'nickname' => 'string',
        'phone' => 'string',
        'idcno' => 'string',
        'deposit' => 'int',
        'balance' => 'int',
        'state' => 'int',
        'create_time' => 'string',
        'last_order_time' => 'string',
        'order_num' => 'int',
        'wx_city' => 'string',
        'wx_avatar_url' => 'string',
        'wx_gender' => 'int',
        'blacklisted' => 'int',
        'blacklisted_expire_time' => 'string',
        'id' => 'string',
        'city_id' => 'string',
        'deposit_order_id' => 'string',
        'channel' => 'string',
        'violation_count' => 'int',
        'wxmp_open_id' => 'string',
        'wx_union_id' => 'string',
        'sex' => 'int',
        'device_os' => 'string',
        'device_sn' => 'string',
        'wx_nickname' => 'string',
        'wx_province' => 'string',
        'membership_expire_time' => 'string',
        'creditscore' => 'int',
        'use_free_endtime' => 'string',
        'ridecard_expire_time' => 'string',
        'ios_unionid' => 'string',
        'score' => 'int',
        'ridecard_count' => 'int'
    ];

    function get_list($city_id, $condition, $pos, $limit)
    {
        $limit = $limit > 20 ? 20 : $limit;

        $sfs = common::array_keys($this->_selectlist);
        $beans = [
        ];

        $st ="";

        if (!empty($city_id)) {
            $st = $st . " and (city_id = ?)";
            $beans[] = $city_id;
        }


        /*
        if (isset($condition['time_interval_type']) && in_array($condition['time_interval_type'], [1, 2])) {
            if ($condition['time_interval_type'] == 1) {
                if (isset($condition['start_time'])) {
                    $st = $st . " and create_time >= ? ";
                    $beans[] = $condition['start_time'];
                }
                if (isset($condition['end_time'])) {
                    $st = $st . " and create_time <= ? ";
                    $beans[] = $condition['end_time'];
                }
            } else if ($condition['time_interval_type'] == 2) {
                if (isset($condition['start_time'])) {
                    $st = $st . " and last_order_time >= ? ";
                    $beans[] = $condition['start_time'];
                }
                if (isset($condition['end_time'])) {
                    $st = $st . " and last_order_time <= ? ";
                    $beans[] = $condition['end_time'];
                }
            }
        }*/
        if (isset($condition['order_overdue'])) {
            if ($condition['order_overdue'] > 0) {
                $st = $st . " and balance < 0";
            } else {
                $st = $st . " and balance >= 0";
            }
        }

        if (isset($condition['to_refund_charge'])) {
            $st = $st . "and balance > 0  and city_id = ?";
            $beans[] = $city_id;
        }

        if (isset($condition['to_refund_membership'])) {
            $st = $st . " and membership_expire_time > ? and city_id = ?";
            $beans[] = date('Y=m-d H:i:s');
            $beans[] = $city_id;
        }

        if (!empty($condition['sk'])) {
            if (common::is_phone($condition['sk'])) {
                $st = $st . " and phone = ?";
                $beans[] = $condition['sk'];
            } else if (IDMan::is_user_id($condition['sk'])) {
                $st = $st . " and id = ?";
                $beans[] = $condition['sk'];
            } else if (common::is_name($condition['sk'])) {
                $st = $st . " and name = ?";
                $beans[] = $condition['sk'];
            } else if (common::is_idcard($condition['sk'])) {
                $st = $st . " and idcno = ?";
                $beans[] = $condition['sk'];
            } else {
                return [];
            }
        } else {
            if (isset($condition['start_time'])) {
                $st = $st . " and create_time >= ? ";
                $beans[] = $condition['start_time'];
            }
            if (isset($condition['end_time'])) {
                $st = $st . " and create_time <= ? ";
                $beans[] = $condition['end_time'];
            }
        }

        if (isset($condition['state'])) {
            if ($condition['state'] != 0) {
                $st = $st . " and blacklisted <> 0";
            } else {
                $st = $st . " and blacklisted = 0";
//$beans[] = $condition['state'];
            }
        }

        $rows = Db::table('tb_user')->whereRaw($st,$beans)->field($sfs)->order('create_time')->limit($pos,$limit)->select();

        return $rows->toArray();
    }

    function query_info($id)
    {
        $sfs = common::array_keys($this->_selectlist);
        $beans = [
            'id' => $id
        ];


        $result = Db::table('tb_user')->where($beans)->field($sfs)->find();
        if (empty($result)) {
            return null;
        }

        $info = $result->toArray();

        if (!empty($info['blacklisted_expire_time']) && time() > strtotime($info['blacklisted_expire_time'])) {
            $this->clear_blacklisted_state($id);
            $info['blacklisted_expire_time'] = null;
            $info['blacklisted'] = 0;
        }

        if (!empty($info['membership_expire_time']) && time() > strtotime($info['membership_expire_time'])) {
            $this->set_membership_expire_time($info['id'], null);
            $info['membership_expire_time'] = null;
        }

        //骑行卡
        if (!empty($info['ridecard_expire_time']) && time() > strtotime($info['ridecard_expire_time'])) {
            $this->set_ridecard_expire_time($info['id'], null,0);
            $info['ridecard_expire_time'] = null;
            $info['ridecard_count'] = 0;
        }


        $this->_info = $info;
        return $info;
    }
    /*
     * */
    function query_info_by_phone($phone)
    {
        $result = Db::table('tb_user')->where(['phone'=>$phone])->field(common::array_keys($this->_selectlist))->find();
        if (empty($result)) {
            return null;
        }

        $info = $result->toArray();

        if (!empty($info['blacklisted_expire_time']) && time() > strtotime($info['blacklisted_expire_time'])) {
            $this->clear_blacklisted_state($info['id']);
            $info['blacklisted_expire_time'] = null;
            $info['blacklisted'] = 0;
        }

        if (!empty($info['membership_expire_time']) && time() > strtotime($info['membership_expire_time'])) {
            $this->set_membership_expire_time($info['id'], null);
            $info['membership_expire_time'] = null;
        }

        //骑行卡
        if (!empty($info['ridecard_expire_time']) && time() > strtotime($info['ridecard_expire_time'])) {
            $this->set_ridecard_expire_time($info['id'], null,0);
            $info['ridecard_expire_time'] = null;
            $info['ridecard_count'] = 0;
        }


        $this->_info = $info;
        return $info;
    }

    function query_info_by_union_id($union_id) {
        $usr_list = Db::table('tb_user')->where('wx_union_id',$union_id)->field(common::array_keys($this->_selectlist))->order('create_time','desc')->find();
        if ($usr_list->isEmpty()){
            return null;
        }

        return $usr_list;
    }

    function query_info_by_ios_union_id($union_id) {
        $usr_list = Db::table('tb_user')->where('wx_union_id',$union_id)->field(common::array_keys($this->_selectlist))->order('create_time','desc')->find();
        if ($usr_list->isEmpty()){
            return null;
        }

        $info = $usr_list->toArray();

        if (!empty($info['blacklisted_expire_time']) && time() > strtotime($info['blacklisted_expire_time'])) {
            $this->clear_blacklisted_state($info['id']);
            $info['blacklisted_expire_time'] = null;
            $info['blacklisted'] = 0;
        }

        if (!empty($info['membership_expire_time']) && time() > strtotime($info['membership_expire_time'])) {
            $this->set_membership_expire_time($info['id'], null);
            $info['membership_expire_time'] = null;
        }

        if (!empty($info['ridecard_expire_time']) && time() > strtotime($info['ridecard_expire_time'])) {
            $this->set_ridecard_expire_time($info['id'], null,0);
            $info['ridecard_expire_time'] = null;
            $info['ridecard_count'] = 0;
        }

        $this->_info = $info;
        return $info;
    }

    function query_info_by_aliuser_id($alipay_user_id) {
        $usr_list = Db::table('tb_user')->where('alipay_user_id',$alipay_user_id)->field(common::array_keys($this->_selectlist))->order('create_time','desc')->find();
        if ($usr_list->isEmpty()){
            return null;
        }

        $info = $usr_list->toArray();

        if (!empty($info['blacklisted_expire_time']) && time() > strtotime($info['blacklisted_expire_time'])) {
            $this->clear_blacklisted_state($info['id']);
            $info['blacklisted_expire_time'] = null;
            $info['blacklisted'] = 0;
        }

        if (!empty($info['membership_expire_time']) && time() > strtotime($info['membership_expire_time'])) {
            $this->set_membership_expire_time($info['id'], null);
            $info['membership_expire_time'] = null;
        }

        if (!empty($info['ridecard_expire_time']) && time() > strtotime($info['ridecard_expire_time'])) {
            $this->set_ridecard_expire_time($info['id'], null,0);
            $info['ridecard_expire_time'] = null;
            $info['ridecard_count'] = 0;
        }

        $this->_info = $info;
        return $info;
    }

    function add($info) {
        $info = common::type_conver($info, $this->_selectlist);
        $ID = IDMan::user_id();
        $info['id'] = $ID;
        $info = array_merge([
            'deposit' => 0,
            'balance' => 0,
            'state' => 0,
            'create_time' => date('Y-m-d H:i:s'),
            'order_num' => 0,
            'is_deleted' => 0,
            'violation_count' => 0,
            'phone' => null,
            'name' => null,
            'idcno' => null,
            'blacklisted' => 0,
        ], $info);

        Db::table('tb_user')->insert($info);
        $this->_info = $info;

        if(isset($info['city_id']))
            (new StatNewModel())->user_incr($info['city_id']);

        return $info;
    }

    function bound_real_name($id, $name, $idcno)
    {
        $info = [
            'name' => $name,
            'idcno' => $idcno,
        ];

        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($info);
    }

    function bound_phone($id, $phone)
    {
        $info = [
            'phone' => $phone,
        ];

        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($info);
    }

    function update_last_order_time($city_id, $id)
    {
        $info = [
            'last_order_time' => date('Y-m-d H:i:s'),
            'city_id' => $city_id,
        ];

        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($info);
    }

    function update_info($id, $data)
    {
        $data = common::type_conver($data, $this->_selectlist);
        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function blacklisted($id, $blacklisted, $blacklisted_expire_time)
    {
        $info = [
            'blacklisted' => $blacklisted,
        ];
        if ($blacklisted > 0) {
            $info['blacklisted_expire_time'] = $blacklisted_expire_time;
        } else {
            $info['blacklisted_expire_time'] = null;
        }

        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($info);
    }

    function ride_violation_punish($user_id, $fine, $blacklisted)
    {
        $user_info = $this->query_info($user_id);
        $data = [
        ];
        if ($fine > 0) {
            $data['balance'] = $user_info['balance'] - $fine;
        }
        if ($blacklisted > 0) {
            $blacklisted_expire_time = null;
            if (empty($user_info['blacklisted_expire_time'])) {
                $blacklisted_expire_time = date('Y-m-d H:i:s', time() + $blacklisted);
            } else {
                $blacklisted_expire_time = date('Y-m-d H:i:s', strtotime($user_info['blacklisted_expire_time']) + $blacklisted);
            }
            $data['blacklisted_expire_time'] = $blacklisted_expire_time;
            $data['blacklisted'] = $blacklisted;
        }

        $where = [
            'id' => $user_id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function clear_blacklisted_state($id)
    {
        $data = [
            'blacklisted' => 0,
            'blacklisted_expire_time' => null,
        ];
        $where = [
            'id' => $id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function ride_violation_revoke($user_id, $fine, $blacklisted)
    {
        $user_info = $this->query_info($user_id);
        $data = [
        ];
        if ($fine > 0) {
            $data['balance'] = $user_info['balance'] + $fine;
        }
        if ($blacklisted > 0) {
            $blacklisted_expire_time = null;
            if (!empty($user_info['blacklisted_expire_time'])) {
                $t = strtotime($user_info['blacklisted_expire_time']) - $blacklisted;
                if ($t >= time()) {
                    $blacklisted_expire_time = date('Y-m-d H:i:s', $t);
                    $blacklisted = $t - time();
                } else {
                    $blacklisted_expire_time = null;
                    $blacklisted = 0;
                }
                $data['blacklisted_expire_time'] = $blacklisted_expire_time;
                $data['blacklisted'] = $blacklisted;
            }
        }

        $where = [
            'id' => $user_id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function incr_balance($user_id, $increment)
    {
        Db::table('tb_user')->where(['id'=>$user_id])->setInc('balance',$increment);

        if ($this->_info && isset($this->_info['balance'])) {
            $this->_info['balance'] += $increment;
        }
    }

    function incr_order_num($user_id)
    {
        Db::table('tb_user')->where(['id'=>$user_id])->setInc('order_num',1);
    }

    function incr_violation_count($user_id)
    {
        Db::table('tb_user')->where(['id'=>$user_id])->setInc('violation_count',1);
    }

    function deposit($user_id, $increment, $order_id)
    {
        Db::table('tb_user')->where(['id'=>$user_id])->inc('deposit',$increment)->update(['deposit_order_id'=>$order_id]);
    }

    function deposit_refund_start($user_id)
    {
        $st = "update tb_user set deposit = 0 - deposit where id = ?";
        $beans = [
            $user_id
        ];
        hisql::db()->execute($st, $beans);
    }

    function deposit_refund_finish($user_id)
    {
        $beans = [
            'id' =>$user_id,
            'deposit' =>['<',0]
        ];
        Db::table('tb_user')->where($beans)->update(['deposit'=>0]);
    }

    function set_membership_expire_time($user_id, $time)
    {
        $data = [
            'membership_expire_time' => $time,
        ];
        $where = [
            'id' => $user_id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function set_ridecard_expire_time($user_id, $time,$count = 0)
    {
        $data = [
            'ridecard_expire_time' => $time,
            'ridecard_count' => $count
        ];
        $where = [
            'id' => $user_id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }

    function set_membership_free_endtime($user_id, $time)
    {
        $data = [
            'membership_expire_time' => $time,
            'use_free_endtime' => $time,
        ];
        $where = [
            'id' => $user_id,
        ];

        Db::table('tb_user')->where($where)->update($data);
    }


    function  query_list($city_id){
        $selectlist = [
            'city_id' => 'string',
            'id' => 'string',
            'name' => 'string',
            'phone' => 'string',
            'membership_expire_time' => 'string',
        ];

        $sfs = common::array_keys($selectlist);
        $beans = [
        ];

        if (!empty($city_id)) {
            $beans['city_id'] = $city_id;

        }

        $rows = Db::table('tb_user')->where($beans)->field($sfs)->order('create_time','desc')->select();
        $l = [];
        foreach ($rows as $row) {
            $l[] = common::sql_row($selectlist, $row);
        }
        return $l;
    }

    function query_info_by_idcno($idcno) {
        $sfs = common::array_keys($this->_selectlist);

        $beans = [
            'idcno' =>$idcno
        ];

        $rows = Db::table('tb_user')->where($beans)->field($sfs)->order('create_time','desc')->find();
        if (empty($rows)) {
            return null;
        }
        $info = $rows->toArray();


        if (!empty($info['blacklisted_expire_time']) && time() > strtotime($info['blacklisted_expire_time'])) {
            $this->clear_blacklisted_state($info['id']);
            $info['blacklisted_expire_time'] = null;
            $info['blacklisted'] = 0;
        }

        if (!empty($info['membership_expire_time']) && time() > strtotime($info['membership_expire_time'])) {
            $this->set_membership_expire_time($info['id'], null);
            $info['membership_expire_time'] = null;
        }

        $this->_info = $info;
        return $info;
    }

    function incr_score($user_id, $increment)
    {
        Db::table('tb_user')->where(['id'=>$user_id])->setInc('score',$increment);
        if ($this->_info && isset($this->_info['score'])) {
            $this->_info['score'] += $increment;
        }
    }

    function addCoupon($userinfo)
    {
        $day = 0;
        $content = '';
        if(isset($userinfo['city_id']))
        {
            if($userinfo['city_id'] == '450981'){
                (new UserMembershipOrderModel())->gift_week_membership($userinfo);
                $day = 30;
                $content = '恭喜获得30日会员卡';
            }

            if($userinfo['city_id'] == '450924'){
                $day = 3;
                $end_time = (new UserMembershipOrderModel())->gift_day_membership($userinfo,$day);
                $this->set_membership_expire_time($userinfo['id'], $end_time);
                $content = '恭喜获得3日会员卡';
                //$duration = 30*24*3600;
//                (new CouponModel())->coupon_for_type_1($userinfo['id'], 500,$duration,'新人福利券');
//                (new CouponModel())->coupon_for_type_1($userinfo['id'], 300,$duration,'新人福利券');
//                (new CouponModel())->coupon_for_type_1($userinfo['id'], 200,$duration,'新人福利券');
            }
            else
            {
                (new CouponModel())->create_coupon_type_1($userinfo['id'], '新人福利券');
            }

        }
        else {
            (new CouponModel())->create_coupon_type_1($userinfo['id'], '新人福利券');
        }

        return [$day,$content];
    }
}